package webservice;

import ejb.facades.TripFacadeLocal;
import ejb.facades.UserFacadeLocal;
import ejb.facades.VehicleFacadeLocal;
import ejb.facades.extra.UserTripParticipationFacadeLocal;
import ejb.facades.forum.TopicFacadeLocal;
import ejb.facades.location.AddressFacadeLocal;
import ejb.facades.location.CountryFacadeLocal;
import entities.Trip;
import entities.User;
import entities.Vehicle;
import entities.extra.UserTripParticipation;
import entities.forum.Topic;
import entities.location.Address;
import entities.location.Country;
import entities.userstate.AwaitingApprovalUser;
import entities.userstate.CancelledUser;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObjectBuilder;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import util.Formater;
import util.Validator;

/**
 * Trabalho PD - 2014/2015
 *
 * @author a21210380 a21210392
 */
@Path("/WebServiceBoleias")
public class WebServiceBoleias {

    @EJB
    private CountryFacadeLocal countryFacade;
    @EJB
    private AddressFacadeLocal addressFacade;
    @EJB
    private TripFacadeLocal tripFacadeLocal;
    @EJB
    private UserFacadeLocal userFacadeLocal;
    @EJB
    private UserTripParticipationFacadeLocal userTripParticipationFacade;
    @EJB
    private VehicleFacadeLocal vehicleFacadeLocal;
    @EJB
    private TopicFacadeLocal topicFacade;

    @GET
    @Path("/getAllCarsFromUser/{user}")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.APPLICATION_JSON)
    public String getAllCarsFromUser(@PathParam("user") int userId) {
        User user = userFacadeLocal.find(userId);
        if (user != null) {
            JsonArrayBuilder createArrayBuilder = Json.createArrayBuilder();
            Collection<Vehicle> vehicles = user.getVehicles();
            if (!vehicles.isEmpty()) {
                for (Vehicle v : vehicles) {
                    JsonObjectBuilder add = Json.createObjectBuilder()
                            .add("id_car", v.getId())
                            .add("brand", v.getBrand())
                            .add("seats", v.getSeats())
                            .add("year", v.getYear())
                            .add("plate_number", v.getPlateNumber());
                    createArrayBuilder.add(add.build());
                }
                return Json.createObjectBuilder().add("cars", createArrayBuilder.build()).build().toString();
            }
        }
        return Json.createObjectBuilder().add("cars", Json.createArrayBuilder().build()).build().toString();
    }

    @GET
    @Path("/getAllTripsFromCar/{car}")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.APPLICATION_JSON)
    public String getAllTripsFromCar(@PathParam("car") int carId) {
        Vehicle v = vehicleFacadeLocal.find(carId);
        if (v != null) {
            List<Trip> findByVehicle = tripFacadeLocal.findByVehicle(v);
            if (!findByVehicle.isEmpty()) {
                JsonArrayBuilder createArrayBuilder = Json.createArrayBuilder();

                for (Trip a : findByVehicle) {
                    JsonObjectBuilder add = Json.createObjectBuilder()
                            .add("id_trip", a.getId())
                            .add("ended", a.isEnded())
                            .add("description", a.getDescription())
                            .add("arrivalDate", Formater.formatDate(a.getArrivalEstimatedDate()))
                            .add("departueDate", Formater.formatDate(a.getDepartueEstimatedDate()))
                            .add("destination_country_name", a.getDestination().getCountry().getCountryName())
                            .add("destination_country_id", a.getDestination().getCountry().getIdCountry())
                            .add("destination_address", a.getDestination().getAddress())
                            .add("destination_address_id", a.getDestination().getId())
                            .add("departue_country_name", a.getDeparture().getCountry().getCountryName())
                            .add("departue_country_id", a.getDeparture().getCountry().getIdCountry())
                            .add("departue_address", a.getDeparture().getAddress())
                            .add("departue_address_id", a.getDeparture().getId())
                            .add("car_id", a.getVehicle().getId())
                            .add("car_brand", a.getVehicle().getBrand());

                    createArrayBuilder.add(add.build());
                }
                return Json.createObjectBuilder().add("trip", createArrayBuilder.build()).build().toString();
            }
        }
        return Json.createObjectBuilder().add("trip", Json.createArrayBuilder().build()).build().toString();
    }

    @GET
    @Path("/getCar/{car}")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.APPLICATION_JSON)
    public String getCar(@PathParam("car") int carId) {
        Vehicle v = vehicleFacadeLocal.find(carId);
        if (v != null) {
            return Json.createObjectBuilder()
                    .add("id_car", v.getId())
                    .add("brand", v.getBrand())
                    .add("seats", v.getSeats())
                    .add("year", v.getYear())
                    .add("plate_number", v.getPlateNumber())
                    .build().toString();
        }
        return "{}";
    }

    @GET
    @Path("/getAllUsers")
    @Produces(MediaType.APPLICATION_JSON)
    public String getAllUsers() {
        List<User> findAll = userFacadeLocal.findAll();
        if (!findAll.isEmpty()) {
            JsonArrayBuilder createArrayBuilder = Json.createArrayBuilder();

            for (User u : findAll) {
                JsonObjectBuilder add = Json.createObjectBuilder()
                        .add("id_user", u.getId())
                        .add("name", u.getName())
                        .add("identity_number", u.getBi())
                        .add("email", u.getEmail())
                        .add("id_address", u.getAddress().getId())
                        .add("id_country", u.getAddress().getCountry().getIdCountry());

                AwaitingApprovalUser awaitingApprovalUser = u.getAwaitingApprovalUser();
                if (awaitingApprovalUser != null) {
                    add.add("awaiting", true)
                            .add("hello_message", awaitingApprovalUser.getHelloMessage());
                } else {
                    add.add("awaiting", false);
                }

                CancelledUser cancelledUser = u.getCancelledUser();
                if (cancelledUser != null) {
                    add.add("cancelled", true)
                            .add("unlock", cancelledUser.isUnlocked())
                            .add("reason", cancelledUser.getCancelingReason())
                            .add("canceling_date", Formater.formatDate(cancelledUser.getCancelingDate()));
                } else {
                    add.add("cancelled", false);
                }

                createArrayBuilder.add(add.build());
            }

            return Json.createObjectBuilder().add("users", createArrayBuilder.build()).build().toString();
        }
        return Json.createObjectBuilder().add("users", Json.createArrayBuilder().build()).build().toString();
    }

    @GET
    @Path("/getUser/{user}")
    @Produces(MediaType.APPLICATION_JSON)
    public String getUser(@PathParam("user") int userId) {
        User u = userFacadeLocal.find(userId);
        if (u != null) {
            JsonObjectBuilder add = Json.createObjectBuilder()
                    .add("id_user", u.getId())
                    .add("name", u.getName())
                    .add("identity_number", u.getBi())
                    .add("email", u.getEmail())
                    .add("id_address", u.getAddress().getId())
                    .add("id_country", u.getAddress().getCountry().getIdCountry());

            AwaitingApprovalUser awaitingApprovalUser = u.getAwaitingApprovalUser();
            if (awaitingApprovalUser != null) {
                add.add("awaiting", true)
                        .add("hello_message", awaitingApprovalUser.getHelloMessage());
            } else {
                add.add("awaiting", false);
            }

            CancelledUser cancelledUser = u.getCancelledUser();
            if (cancelledUser != null) {
                add.add("cancelled", true)
                        .add("unlock", cancelledUser.isUnlocked())
                        .add("reason", cancelledUser.getCancelingReason())
                        .add("canceling_date", Formater.formatDate(cancelledUser.getCancelingDate()));
            } else {
                add.add("cancelled", false);
            }

            return add.build().toString();
        }
        return "{}";
    }

    @GET
    @Path("/getCountrys")
    @Produces(MediaType.APPLICATION_JSON)
    public String getCountrys() {
        List<Country> resultList = countryFacade.findAll();
        if (!resultList.isEmpty()) {
            JsonArrayBuilder createArrayBuilder = Json.createArrayBuilder();

            for (Country c : resultList) {
                JsonObjectBuilder add = Json.createObjectBuilder().add("id_country", c.getIdCountry()).add("name", c.getCountryName());
                createArrayBuilder.add(add.build());
            }

            return Json.createObjectBuilder().add("country", createArrayBuilder.build()).build().toString();
        }

        return Json.createObjectBuilder().add("country", Json.createArrayBuilder().build()).build().toString();
    }

    @GET
    @Path("/getAllAddress")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.APPLICATION_JSON)
    public String getAllAddress() {
        List<Address> findAllAddressFromCountry = addressFacade.findAll();
        if (!findAllAddressFromCountry.isEmpty()) {
            JsonArrayBuilder createArrayBuilder = Json.createArrayBuilder();

            for (Address a : findAllAddressFromCountry) {
                JsonObjectBuilder add = Json.createObjectBuilder().add("id_country", a.getCountry().getIdCountry()).add("id_address", a.getId()).add("address", a.getAddress()).add("lat", a.getLat()).add("log", a.getLog());
                createArrayBuilder.add(add.build());
            }
            return Json.createObjectBuilder().add("address", createArrayBuilder.build()).build().toString();
        }
        return "{\"address\":[]}";
    }

    @GET
    @Path("/getAllAddressFromCountry/{country}")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.APPLICATION_JSON)
    public String getAllAddressFromCountry(@PathParam("country") int countryId) {
        Country findCountry = countryFacade.find(countryId);
        if (findCountry != null) {
            List<Address> findAllAddressFromCountry = addressFacade.findAllAddressFromCountry(findCountry);
            if (!findAllAddressFromCountry.isEmpty()) {
                JsonArrayBuilder createArrayBuilder = Json.createArrayBuilder();

                for (Address a : findAllAddressFromCountry) {
                    JsonObjectBuilder add = Json.createObjectBuilder().add("id_country", a.getCountry().getIdCountry()).add("id_address", a.getId()).add("address", a.getAddress()).add("lat", a.getLat()).add("log", a.getLog());
                    createArrayBuilder.add(add.build());
                }
                return Json.createObjectBuilder().add("address", createArrayBuilder.build()).build().toString();
            }
        }
        return "{\"address\":[]}";
    }

    @GET
    @Path("/getAllRecentTopics/{top}")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.APPLICATION_JSON)
    public String getAllRecentTopics(@PathParam("top") int top) {
        List<Topic> allTopics = topicFacade.findRecent(top);
        if (!allTopics.isEmpty()) {
            JsonArrayBuilder createArrayBuilder = Json.createArrayBuilder();

            for (Topic t : allTopics) {
                JsonObjectBuilder add = Json.createObjectBuilder()
                        .add("id_topic", t.getId())
                        .add("created_date", Formater.formatDate(t.getCreatedDate()))
                        .add("message", t.getMessage())
                        .add("title", t.getTitle())
                        .add("id_user", t.getUser().getId())
                        .add("name_user", t.getUser().getName());

                createArrayBuilder.add(add.build());
            }
            return Json.createObjectBuilder().add("topic", createArrayBuilder.build()).build().toString();
        }
        return "{\"topic\":[]}";
    }

    @GET
    @Path("/getAllRecentTrips/{top}")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.APPLICATION_JSON)
    public String getAllRecentTrips(@PathParam("top") int top) {
        List<Trip> findAllAddressFromCountry = tripFacadeLocal.findRecent(top);
        if (!findAllAddressFromCountry.isEmpty()) {
            JsonArrayBuilder createArrayBuilder = Json.createArrayBuilder();

            for (Trip a : findAllAddressFromCountry) {
                JsonObjectBuilder add = Json.createObjectBuilder()
                        .add("id_trip", a.getId())
                        .add("ended", a.isEnded())
                        .add("description", a.getDescription())
                        .add("arrivalDate", Formater.formatDate(a.getArrivalEstimatedDate()))
                        .add("departueDate", Formater.formatDate(a.getDepartueEstimatedDate()))
                        .add("destination_country_name", a.getDestination().getCountry().getCountryName())
                        .add("destination_country_id", a.getDestination().getCountry().getIdCountry())
                        .add("destination_address", a.getDestination().getAddress())
                        .add("destination_address_id", a.getDestination().getId())
                        .add("departue_country_name", a.getDeparture().getCountry().getCountryName())
                        .add("departue_country_id", a.getDeparture().getCountry().getIdCountry())
                        .add("departue_address", a.getDeparture().getAddress())
                        .add("departue_address_id", a.getDeparture().getId())
                        .add("car_id", a.getVehicle().getId())
                        .add("car_brand", a.getVehicle().getBrand());

                createArrayBuilder.add(add.build());
            }
            return Json.createObjectBuilder().add("trip", createArrayBuilder.build()).build().toString();
        }
        return "{\"trip\": []}";
    }

    @GET
    @Path("/getAllTripsCreatedBy/{user}")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.APPLICATION_JSON)
    public String getAllTripsCreatedBy(@PathParam("user") int userId) {
        User user = userFacadeLocal.find(userId);
        if (user != null) {
            List<Trip> findAllAddressFromCountry = tripFacadeLocal.findAllTripsCreatedBy(user);
            if (!findAllAddressFromCountry.isEmpty()) {
                JsonArrayBuilder createArrayBuilder = Json.createArrayBuilder();

                for (Trip a : findAllAddressFromCountry) {
                    JsonObjectBuilder add = Json.createObjectBuilder()
                            .add("id_trip", a.getId())
                            .add("ended", a.isEnded())
                            .add("description", a.getDescription())
                            .add("arrivalDate", Formater.formatDate(a.getArrivalEstimatedDate()))
                            .add("departueDate", Formater.formatDate(a.getDepartueEstimatedDate()))
                            .add("destination_country_name", a.getDestination().getCountry().getCountryName())
                            .add("destination_country_id", a.getDestination().getCountry().getIdCountry())
                            .add("destination_address", a.getDestination().getAddress())
                            .add("destination_address_id", a.getDestination().getId())
                            .add("departue_country_name", a.getDeparture().getCountry().getCountryName())
                            .add("departue_country_id", a.getDeparture().getCountry().getIdCountry())
                            .add("departue_address", a.getDeparture().getAddress())
                            .add("departue_address_id", a.getDeparture().getId())
                            .add("car_id", a.getVehicle().getId())
                            .add("car_brand", a.getVehicle().getBrand());

                    createArrayBuilder.add(add.build());
                }
                return Json.createObjectBuilder().add("trip", createArrayBuilder.build()).build().toString();
            }
        }
        return "{\"trip\":[]}";
    }

    @GET
    @Path("/getAllAppointedTripsFor/{user}")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.APPLICATION_JSON)
    public String getAllAppointedTripsFor(@PathParam("user") int userId) {
        User user = userFacadeLocal.find(userId);
        if (user != null) {
            List<UserTripParticipation> findAllAddressFromCountry = userTripParticipationFacade.findAppointedTripFor(user);
            if (!findAllAddressFromCountry.isEmpty()) {
                JsonArrayBuilder createArrayBuilder = Json.createArrayBuilder();

                for (UserTripParticipation userTripParticipation : findAllAddressFromCountry) {
                    Trip a = userTripParticipation.getTrip();
                    JsonObjectBuilder add = Json.createObjectBuilder()
                            .add("id_trip", a.getId())
                            .add("ended", a.isEnded())
                            .add("accepted", userTripParticipation.getAccepted())
                            .add("description", a.getDescription())
                            .add("arrivalDate", Formater.formatDate(a.getArrivalEstimatedDate()))
                            .add("departueDate", Formater.formatDate(a.getDepartueEstimatedDate()))
                            .add("destination_country_name", a.getDestination().getCountry().getCountryName())
                            .add("destination_country_id", a.getDestination().getCountry().getIdCountry())
                            .add("destination_address", a.getDestination().getAddress())
                            .add("destination_address_id", a.getDestination().getId())
                            .add("departue_country_name", a.getDeparture().getCountry().getCountryName())
                            .add("departue_country_id", a.getDeparture().getCountry().getIdCountry())
                            .add("departue_address", a.getDeparture().getAddress())
                            .add("departue_address_id", a.getDeparture().getId())
                            .add("car_id", a.getVehicle().getId())
                            .add("car_brand", a.getVehicle().getBrand());

                    createArrayBuilder.add(add.build());
                }
                return Json.createObjectBuilder().add("trip", createArrayBuilder.build()).build().toString();
            }
        }
        return "{\"trip\":[]}";
    }

    @GET
    @Path("/getAllTripsHistoricFor/{user}")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.APPLICATION_JSON)
    public String getAllTripsHistoricFor(@PathParam("user") int userId) {
        User user = userFacadeLocal.find(userId);
        if (user != null) {
            List<UserTripParticipation> findAllAddressFromCountry = userTripParticipationFacade.findTripHistoricFor(user);
            if (!findAllAddressFromCountry.isEmpty()) {
                JsonArrayBuilder createArrayBuilder = Json.createArrayBuilder();

                for (UserTripParticipation userTripParticipation : findAllAddressFromCountry) {
                    Trip a = userTripParticipation.getTrip();
                    JsonObjectBuilder add = Json.createObjectBuilder()
                            .add("id_trip", a.getId())
                            .add("ended", a.isEnded())
                            .add("accepted", userTripParticipation.getAccepted())
                            .add("description", a.getDescription())
                            .add("arrivalDate", Formater.formatDate(a.getArrivalEstimatedDate()))
                            .add("departueDate", Formater.formatDate(a.getDepartueEstimatedDate()))
                            .add("destination_country_name", a.getDestination().getCountry().getCountryName())
                            .add("destination_country_id", a.getDestination().getCountry().getIdCountry())
                            .add("destination_address", a.getDestination().getAddress())
                            .add("destination_address_id", a.getDestination().getId())
                            .add("departue_country_name", a.getDeparture().getCountry().getCountryName())
                            .add("departue_country_id", a.getDeparture().getCountry().getIdCountry())
                            .add("departue_address", a.getDeparture().getAddress())
                            .add("departue_address_id", a.getDeparture().getId())
                            .add("car_id", a.getVehicle().getId())
                            .add("car_brand", a.getVehicle().getBrand());

                    createArrayBuilder.add(add.build());
                }
                return Json.createObjectBuilder().add("trip", createArrayBuilder.build()).build().toString();
            }
        }
        return "{\"trip\":[]}";
    }

    @GET
    @Path("/getAllMissingEvaluationsFor/{user}")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.APPLICATION_JSON)
    public String getAllMissingEvaluationsFor(@PathParam("user") int userId) {
        User user = userFacadeLocal.find(userId);
        if (user != null) {
            Collection<Trip> evaluations = user.getEvaluations();
            if (!evaluations.isEmpty()) {
                JsonArrayBuilder createArrayBuilder = Json.createArrayBuilder();

                for (Trip a : evaluations) {
                    JsonObjectBuilder add = Json.createObjectBuilder()
                            .add("id_trip", a.getId())
                            .add("ended", a.isEnded())
                            .add("description", a.getDescription())
                            .add("arrivalDate", Formater.formatDate(a.getArrivalEstimatedDate()))
                            .add("departueDate", Formater.formatDate(a.getDepartueEstimatedDate()))
                            .add("destination_country_name", a.getDestination().getCountry().getCountryName())
                            .add("destination_country_id", a.getDestination().getCountry().getIdCountry())
                            .add("destination_address", a.getDestination().getAddress())
                            .add("destination_address_id", a.getDestination().getId())
                            .add("departue_country_name", a.getDeparture().getCountry().getCountryName())
                            .add("departue_country_id", a.getDeparture().getCountry().getIdCountry())
                            .add("departue_address", a.getDeparture().getAddress())
                            .add("departue_address_id", a.getDeparture().getId())
                            .add("car_id", a.getVehicle().getId())
                            .add("car_brand", a.getVehicle().getBrand());

                    createArrayBuilder.add(add.build());
                }
                return Json.createObjectBuilder().add("trip", createArrayBuilder.build()).build().toString();
            }
        }
        return "{\"trip\":[]}";
    }

    @GET
    @Path("/searchTrips/start={start}&date={date}")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.APPLICATION_JSON)
    public String searchTripsByStartingPlace(@PathParam("start") int start, @PathParam("date") long date) {
        Address find = addressFacade.find(start);
        if (find != null) {
            List<Trip> findAllAddressFromCountry = tripFacadeLocal.searchTrips(find, Validator.parseDate(date));
            if (!findAllAddressFromCountry.isEmpty()) {
                JsonArrayBuilder createArrayBuilder = Json.createArrayBuilder();

                for (Trip a : findAllAddressFromCountry) {
                    JsonObjectBuilder add = Json.createObjectBuilder()
                            .add("id_trip", a.getId())
                            .add("ended", a.isEnded())
                            .add("description", a.getDescription())
                            .add("arrivalDate", Formater.formatDate(a.getArrivalEstimatedDate()))
                            .add("departueDate", Formater.formatDate(a.getDepartueEstimatedDate()))
                            .add("destination_country_name", a.getDestination().getCountry().getCountryName())
                            .add("destination_country_id", a.getDestination().getCountry().getIdCountry())
                            .add("destination_address", a.getDestination().getAddress())
                            .add("destination_address_id", a.getDestination().getId())
                            .add("departue_country_name", a.getDeparture().getCountry().getCountryName())
                            .add("departue_country_id", a.getDeparture().getCountry().getIdCountry())
                            .add("departue_address", a.getDeparture().getAddress())
                            .add("departue_address_id", a.getDeparture().getId())
                            .add("car_id", a.getVehicle().getId())
                            .add("car_brand", a.getVehicle().getBrand());

                    createArrayBuilder.add(add.build());
                }
                return Json.createObjectBuilder().add("trip", createArrayBuilder.build()).build().toString();
            }
        }
        return "{\"trip\":[]}";
    }

    @GET
    @Path("/searchTrips/end={end}")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.APPLICATION_JSON)
    public String searchTrips(@PathParam("end") int end) {
        Address find = addressFacade.find(end);
        if (find != null) {
            List<Trip> findAllAddressFromCountry = tripFacadeLocal.searchTrips(find);
            if (!findAllAddressFromCountry.isEmpty()) {
                JsonArrayBuilder createArrayBuilder = Json.createArrayBuilder();

                for (Trip a : findAllAddressFromCountry) {
                    JsonObjectBuilder add = Json.createObjectBuilder()
                            .add("id_trip", a.getId())
                            .add("ended", a.isEnded())
                            .add("description", a.getDescription())
                            .add("arrivalDate", Formater.formatDate(a.getArrivalEstimatedDate()))
                            .add("departueDate", Formater.formatDate(a.getDepartueEstimatedDate()))
                            .add("destination_country_name", a.getDestination().getCountry().getCountryName())
                            .add("destination_country_id", a.getDestination().getCountry().getIdCountry())
                            .add("destination_address", a.getDestination().getAddress())
                            .add("destination_address_id", a.getDestination().getId())
                            .add("departue_country_name", a.getDeparture().getCountry().getCountryName())
                            .add("departue_country_id", a.getDeparture().getCountry().getIdCountry())
                            .add("departue_address", a.getDeparture().getAddress())
                            .add("departue_address_id", a.getDeparture().getId())
                            .add("car_id", a.getVehicle().getId())
                            .add("car_brand", a.getVehicle().getBrand());

                    createArrayBuilder.add(add.build());
                }
                return Json.createObjectBuilder().add("trip", createArrayBuilder.build()).build().toString();
            }
        }
        return "{\"trip\":[]}";
    }

    @GET
    @Path("/searchTrips/date={date}")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.APPLICATION_JSON)
    public String searchTrips(@PathParam("date") long date) {
        Date parseDate = Validator.parseDate(date);
        if(parseDate == null) {
            return "{\"trip\":[]}";
        }
        List<Trip> findAllAddressFromCountry = tripFacadeLocal.searchTrips(parseDate);
        if (!findAllAddressFromCountry.isEmpty()) {
            JsonArrayBuilder createArrayBuilder = Json.createArrayBuilder();

            for (Trip a : findAllAddressFromCountry) {
                JsonObjectBuilder add = Json.createObjectBuilder()
                        .add("id_trip", a.getId())
                        .add("ended", a.isEnded())
                        .add("description", a.getDescription())
                        .add("arrivalDate", Formater.formatDate(a.getArrivalEstimatedDate()))
                        .add("departueDate", Formater.formatDate(a.getDepartueEstimatedDate()))
                        .add("destination_country_name", a.getDestination().getCountry().getCountryName())
                        .add("destination_country_id", a.getDestination().getCountry().getIdCountry())
                        .add("destination_address", a.getDestination().getAddress())
                        .add("destination_address_id", a.getDestination().getId())
                        .add("departue_country_name", a.getDeparture().getCountry().getCountryName())
                        .add("departue_country_id", a.getDeparture().getCountry().getIdCountry())
                        .add("departue_address", a.getDeparture().getAddress())
                        .add("departue_address_id", a.getDeparture().getId())
                        .add("car_id", a.getVehicle().getId())
                        .add("car_brand", a.getVehicle().getBrand());

                createArrayBuilder.add(add.build());
            }
            return Json.createObjectBuilder().add("trip", createArrayBuilder.build()).build().toString();
        }
        return "{\"trip\":[]}";
    }

    @GET
    @Path("/searchTrips/start={start}&end={end}&date={date}")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.APPLICATION_JSON)
    public String searchTrips(@PathParam("start") int start, @PathParam("end") int end, @PathParam("date") long date) {
        Address startAddress = addressFacade.find(start);
        if (startAddress == null) {
            return "{\"trip\":[]}";
        }
        Address endAddress = addressFacade.find(end);
        if (endAddress == null) {
            return "{\"trip\":[]}";
        }
        List<Trip> findAllAddressFromCountry = tripFacadeLocal.searchTrips(startAddress, endAddress, Validator.parseDate(date));
        if (!findAllAddressFromCountry.isEmpty()) {
            JsonArrayBuilder createArrayBuilder = Json.createArrayBuilder();

            for (Trip a : findAllAddressFromCountry) {
                JsonObjectBuilder add = Json.createObjectBuilder()
                        .add("id_trip", a.getId())
                        .add("ended", a.isEnded())
                        .add("description", a.getDescription())
                        .add("arrivalDate", Formater.formatDate(a.getArrivalEstimatedDate()))
                        .add("departueDate", Formater.formatDate(a.getDepartueEstimatedDate()))
                        .add("destination_country_name", a.getDestination().getCountry().getCountryName())
                        .add("destination_country_id", a.getDestination().getCountry().getIdCountry())
                        .add("destination_address", a.getDestination().getAddress())
                        .add("destination_address_id", a.getDestination().getId())
                        .add("departue_country_name", a.getDeparture().getCountry().getCountryName())
                        .add("departue_country_id", a.getDeparture().getCountry().getIdCountry())
                        .add("departue_address", a.getDeparture().getAddress())
                        .add("departue_address_id", a.getDeparture().getId())
                        .add("car_id", a.getVehicle().getId())
                        .add("car_brand", a.getVehicle().getBrand());

                createArrayBuilder.add(add.build());
            }
            return Json.createObjectBuilder().add("trip", createArrayBuilder.build()).build().toString();
        }
        return "{\"trip\":[]}";
    }

    @GET
    @Path("/getLastOnlineUsers")
    @Produces(MediaType.APPLICATION_JSON)
    public String getLastOnlineUsers() {
        List<User> findAll = userFacadeLocal.getLastOnlineUsers();
        if (!findAll.isEmpty()) {
            JsonArrayBuilder createArrayBuilder = Json.createArrayBuilder();

            for (User u : findAll) {
                JsonObjectBuilder add = Json.createObjectBuilder()
                        .add("id_user", u.getId())
                        .add("name", u.getName())
                        .add("identity_number", u.getBi())
                        .add("email", u.getEmail())
                        .add("id_address", u.getAddress().getId())
                        .add("id_country", u.getAddress().getCountry().getIdCountry());

                AwaitingApprovalUser awaitingApprovalUser = u.getAwaitingApprovalUser();
                if (awaitingApprovalUser != null) {
                    add.add("awaiting", true)
                            .add("hello_message", awaitingApprovalUser.getHelloMessage());
                } else {
                    add.add("awaiting", false);
                }

                CancelledUser cancelledUser = u.getCancelledUser();
                if (cancelledUser != null) {
                    add.add("cancelled", true)
                            .add("unlock", cancelledUser.isUnlocked())
                            .add("reason", cancelledUser.getCancelingReason())
                            .add("canceling_date", Formater.formatDate(cancelledUser.getCancelingDate()));
                } else {
                    add.add("cancelled", false);
                }

                createArrayBuilder.add(add.build());
            }

            return Json.createObjectBuilder().add("users", createArrayBuilder.build()).build().toString();
        }
        return Json.createObjectBuilder().add("users", Json.createArrayBuilder().build()).build().toString();
    }
}
